package com.adcc.swingui.demo.menubar;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JMenu;

import com.adcc.swingui.base.DefaultMenuBar;
import com.jidesoft.docking.DockableFrame;
import com.jidesoft.docking.DockableHolder;
import com.jidesoft.swing.JideMenu;
/**
 * 窗口菜单
 * @author charlie
 *
 */
public class WindowMenuBut extends DefaultMenuBar {

	public WindowMenuBut(Container container) {
		super(container);
	}

	@Override
	protected JComponent build() {
		final JMenu menu = new JideMenu("窗口");
        menu.setMnemonic('W');
        //动态生成菜单，更具面板
        for (String string : ((DockableHolder)container).getDockingManager().getAllFrameNames()) {
        	final DockableFrame  ff =  ((DockableHolder)container).getDockingManager().getFrame(string);
        	final JCheckBoxMenuItem item = new JCheckBoxMenuItem(ff.getTitle());
        	item.setName(ff.getName());
        	item.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				if(item.isSelected()) {
    					((DockableHolder) container).getDockingManager().showFrame(item.getName());
    				} else {
    					((DockableHolder) container).getDockingManager().hideFrame(item.getName());
    				}
    				
    			}
    		});
        	addChildMenu(menu, item, null);
        }
        //设置菜单的选择状态
        menu.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseEntered(MouseEvent e) {
        		//设置窗口
        		List<String> names = ((DockableHolder)container).getDockingManager().getAllFrameNames();
        		for (int i = 0; i < names.size(); i++) {
        			final DockableFrame  ff =  ((DockableHolder)container).getDockingManager().
        					getFrame(names.get(i));
        			final JCheckBoxMenuItem item = (JCheckBoxMenuItem)menu.getMenuComponent(i);
        			item.setSelected(ff.isVisible());
				}
        	}
        	
		});
        return menu;
	}

	@Override
	protected void offLineVisiable(List<JComponent> jcomps) {
		// TODO Auto-generated method stub
		
	}
}
